﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.Windows.Interop;
using WCintiqHID;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;

namespace TTIServer
{
	/// <summary>
	/// Interaction logic for PASLayout.xaml
	/// </summary>
	public partial class PASLayout : UserControl
	{


		Cintiq_K myWC21ux;
		Cintiq_K myWC24hd;
		ScreenFactorClass theSFC;

		const int displayCanvasHeight = 262;
		const int displayCanvasWidth = 420;

		double CurrentScreenHeight = SystemParameters.PrimaryScreenHeight;
		double CurrentScreenWidth = SystemParameters.PrimaryScreenWidth;

		double CintiqHeight;
		double CintiqWidth;

		System.Windows.Shapes.Rectangle PASRectangle;
		
		private System.Windows.Point MouseDownLocation;
		private System.Windows.Point MouseUpLocation;
		private System.Windows.Point MouseDownLocationOnRect;
		
		Boolean ClickedMouseOnRectangle;

		public delegate void PASRectangle_MouseDownHandler(object sender, MouseEventArgs e);

		private Thickness PASRectThickness;

		private static BitmapSource CopyPrimaryScreen()
		{
			using (var screenBmp = new Bitmap(
				(int)SystemParameters.PrimaryScreenWidth,
				(int)SystemParameters.PrimaryScreenHeight,
				System.Drawing.Imaging.PixelFormat.Format32bppArgb))
			{
				using (var bmpGraphics = Graphics.FromImage(screenBmp))
				{
					bmpGraphics.CopyFromScreen(0, 0, 0, 0, screenBmp.Size);
					return Imaging.CreateBitmapSourceFromHBitmap(
						screenBmp.GetHbitmap(),
						IntPtr.Zero,
						Int32Rect.Empty,
						BitmapSizeOptions.FromEmptyOptions());
				}
			}
		}

		private static bool IsTextAllowed(string text)
		{
			Regex regex = new Regex("[^0-9]+"); //regex that matches disallowed text
			return !regex.IsMatch(text);
		}

		public PASLayout(Cintiq_K wCintiqRef, ScreenFactorClass mySF)
		{
			this.InitializeComponent();


			myWC21ux = wCintiqRef;

			DisplayImg.BeginInit();
			DisplayImg.Source = CopyPrimaryScreen();
			DisplayImg.EndInit();
			//TODO: Get the Values from Whoever!
			theSFC = mySF;
			// Insert code required on object creation below this point.
			GetFactorValues();
			MouseUpLocation.X = 0;
			MouseUpLocation.Y = 0;
			
			
			

		}

		public void GetFactorValues()
		{
			theSFC = myWC21ux.GetCurrentScreenFactors();
			
			txtOffsetX.Text = theSFC.CintiqXLoc.ToString();
			txtOffsetY.Text = theSFC.CintiqYLoc.ToString();
			txtPixelsX.Text = SystemParameters.PrimaryScreenWidth.ToString();
			txtPixelsY.Text = SystemParameters.PrimaryScreenHeight.ToString();
			sfEnable.IsChecked = theSFC.Enable;

			PASRectangle = new System.Windows.Shapes.Rectangle();
			switch (myWC21ux.GetPID())
			{
				case 0xCC:
					CintiqHeight = 1200;
					CintiqWidth = 1600;
					break;
				case 0xF8:
					CintiqHeight = 1200;
					CintiqWidth = 1920;
					break;
			}
			PASRectangle.Width = displayCanvasWidth / (SystemParameters.PrimaryScreenWidth / CintiqWidth);
			PASRectangle.Height = displayCanvasHeight / (SystemParameters.PrimaryScreenHeight / CintiqHeight);
			PASRectangle.StrokeThickness = 1;
			PASRectangle.Opacity = 0.85;
			PASRectangle.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromRgb(0xFF, 0, 0));
			PASRectangle.Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(0xff, 0x97, 0x97, 0x97));
			PASRectangle.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
			PASRectangle.RenderTransform = new TranslateTransform(theSFC.CintiqXLoc, theSFC.CintiqYLoc);
			//PASRectangle.Cursor = Cursors.SizeAll;

			PASRectangle.MouseLeftButtonDown += new MouseButtonEventHandler(PASRectangle_MouseLeftButtonDown);
			PASRectangle.MouseMove += new MouseEventHandler(PASRectangle_MouseMove);
			PASRectangle.MouseLeftButtonUp += new MouseButtonEventHandler(PASRectangle_MouseLeftButtonUp);

			DisplayCanvas.Children.Add(PASRectangle);

			txtOffsetX.TextChanged += txtOffsetX_TextChanged;
			txtOffsetY.TextChanged += txtOffsetY_TextChanged;

		}



		private void PASRectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{


			MouseDownLocation = e.GetPosition(this);
			ClickedMouseOnRectangle = true;

		}

		private void PASRectangle_MouseMove(object sender, MouseEventArgs e)
		{

			if ((e.LeftButton == MouseButtonState.Pressed) && ( ClickedMouseOnRectangle == true) )
			{

				
			}

		}

		private void PASRectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{

			MouseUpLocation = e.GetPosition(this);
			MouseDownLocationOnRect = e.GetPosition(PASRectangle);
			ClickedMouseOnRectangle = false;

		}

		private void RadioButton_Checked(object sender, RoutedEventArgs e)
		{

		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{



			theSFC.CintiqXLoc = UInt16.Parse(txtOffsetX.Text);
			theSFC.CintiqYLoc = UInt16.Parse(txtOffsetY.Text);
			theSFC.YFactor = (UInt16)((SystemParameters.PrimaryScreenHeight * 1000) / CintiqHeight);
			theSFC.XFactor = (UInt16)((SystemParameters.PrimaryScreenWidth * 1000) / CintiqWidth);
			theSFC.Enable = (bool)sfEnable.IsChecked;

			myWC21ux.SendNewDisplayCoords(theSFC);
			//this.Hide();


		}

		private void txtPixelsY_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (txtOffsetY.Text != "")
				PASRectangle.RenderTransform = new TranslateTransform(0, int.Parse(txtOffsetY.Text));

		}

		private void txtOffsetX_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (txtOffsetX.Text != "")
				PASRectangle.RenderTransform = new TranslateTransform(int.Parse(txtOffsetX.Text), 0);
			
		}

		private void txtOffsetY_TextChanged(object sender, TextChangedEventArgs e)
		{

		}

		private void txtOffsetX_PreviewTextInput(object sender, TextCompositionEventArgs e)
		{
			e.Handled = !IsTextAllowed(e.Text);
		}

		private void txtOffsetY_PreviewTextInput(object sender, TextCompositionEventArgs e)
		{
			e.Handled = !IsTextAllowed(e.Text);
		}

	}
}